package com.huawei.esdk.uc.sametime.chatwindow.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Menu;

import com.huawei.esdk.uc.sametime.ConstantsDefines;
import com.huawei.esdk.uc.sametime.ConstantsDefines.CallStatus;
import com.huawei.esdk.uc.sametime.international.Messages;
import com.huawei.esdk.uc.sametime.services.UCCallService;
import com.huawei.esdk.uc.sametime.services.UCSettingsService;
import com.huawei.esdk.uc.sametime.util.GlobalConfiguration;
import com.huawei.esdk.uc.sametime.widgets.CallMemuUtil;
import com.ibm.collaboration.realtime.chat.actions.ChatActionWindowDelegate;
import com.ibm.collaboration.realtime.chatwindow.ChatWindowHandler;
import com.ibm.collaboration.realtime.people.Person;

/**
 * 聊天窗口上视频呼叫按钮的响应处理类 A contributed action that shows / hides the chat pop-up
 * add-on.
 * @author y00175154
 */

public class EsdkVideoCallChatAddOnOpenAction implements
        ChatActionWindowDelegate
{

    private CallMemuUtil menuCreator;

    private Person[] participants;

    public EsdkVideoCallChatAddOnOpenAction()
    {

    }

    public void run(IAction action, ChatWindowHandler handler)
    {
        if (!handler.isPopupVisible())
        {
            // 这里先判断UC是否登录：
            // 如果未登录，则不执行以下操作，界面弹出"插件未登录UC,无法使用该功能"
            participants = handler.getParticipants();
            if (participants != null)
            {
                menuCreator = new CallMemuUtil(handler, true);
                menuCreator.setSelectionPerson(participants);
                action.setMenuCreator(menuCreator);
                Menu menu = menuCreator.getMenu(handler.getShell());
                if (!GlobalConfiguration.getInstance().isSamtimeLogin()
                        || !GlobalConfiguration.getInstance().isUCLogin())
                {
                    for (int i = 0; i < menu.getItemCount(); i++)
                    {
                        menu.getItem(i).setEnabled(false);
                    }
                    return;
                }
                for (int i = 0; i < menu.getItemCount(); i++)
                {
                    if (CallStatus.end != UCCallService.getCallStatus()
                            || ConstantsDefines.IP_PHONE_MODE
                                    .equals(UCSettingsService.getPhoneJoinDev()))
                    {
                        menu.getItem(i).setEnabled(false);
                    }
                    else
                    {
                        menu.getItem(i).setEnabled(true);
                    }
                }
                menu.setVisible(true);
            }
        }
        else
        {
            MessageDialog
                    .openError(
                            handler.getShell(),
                            Messages.ERROR_TIP,
                            Messages.EsdkConfChatAddOnOpenAction_CANNOT_OPEN_SECOND_WINDOW);
        }
    }
}
